﻿using LanguageSchool.Model;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace LanguageSchool
{
    /// <summary>
    /// Логика взаимодействия для MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private DateTime currentDate;

        public MainWindow()
        {
            InitializeComponent();
            currentDate = DateTime.Now;
            lblMonth.Content = GlobalSettings.Months[currentDate.Month];
            calendar.CurrentDate = currentDate;
            calendar.Schedules = LinksProvider.Context.Shedules.ToList();

            grdStudents.ItemsSource = null;
            grdStudents.ItemsSource = GetStudents();

            grdTeachers.ItemsSource = null;
            grdTeachers.ItemsSource = GetTeachers();
            Shedule s;
            //this.context = new LanguageSchoolEntities1();
            //context.Users.Add(new Users { FirstName = "Pshe" });
            //Group g = new Group();
            //Shedule sh = new Shedule();
            //sh.Group = g;
            ////sh.
            //context.SaveChanges();
        }
        



        private IEnumerable<Student> GetStudents(string nameFilter = null)
        {
            if (string.IsNullOrWhiteSpace(nameFilter))
            {
                return LinksProvider.Context.Students.ToList();
            }
            if (!IsArchieved.IsChecked.Value)
            {
                return LinksProvider.Context.Students.ToList()
                    .Where(s => s.Name.IndexOf(nameFilter, StringComparison.OrdinalIgnoreCase) != -1).ToList();
            }
            else
            {
                return LinksProvider.Context.Students.ToList()
                    .Where(s => s.IsArchieved == true && s.Name.IndexOf(nameFilter, StringComparison.OrdinalIgnoreCase) != -1).ToList();
            }
        }
        private IEnumerable<Student> GetStudents(bool isArchived)
        {
            if (isArchived)
            {
                return LinksProvider.Context.Students.ToList()
                    .Where(s => s.IsArchieved == isArchived && s.Name.IndexOf(txtFilter.Text, StringComparison.OrdinalIgnoreCase) != -1).ToList();
            }
            else
            {
                return LinksProvider.Context.Students.ToList()
                    .Where(s => s.Name.IndexOf(txtFilter.Text, StringComparison.OrdinalIgnoreCase) != -1).ToList();
            }
        }


        private IEnumerable<Teacher> GetTeachers(string nameFilter = null)
        {
            if (string.IsNullOrWhiteSpace(nameFilter))
            {
                return LinksProvider.Context.Teachers.ToList();
            }
            return LinksProvider.Context.Teachers.ToList()
                .Where(s => s.Name.IndexOf(nameFilter, StringComparison.OrdinalIgnoreCase) != -1).ToList();            
        }

        private void AddStudent_Click(object sender, RoutedEventArgs e)
        {
            Student student = new Student();
            AddStudentWindow asw = new AddStudentWindow(student);
            asw.Owner = this;
            var res = asw.ShowDialog();
            if (res == true)
            {
                LinksProvider.Context.Students.Add(student);
                LinksProvider.Context.SaveChanges();
                grdStudents.ItemsSource = null;
                grdStudents.ItemsSource = GetStudents(txtFilter.Text);
            }
        }

        private void Students_GotFocus(object sender, RoutedEventArgs e)
        {
        //    grdStudents.ItemsSource = null;
        //    grdStudents.ItemsSource = GetStudents();
        }


        private void StudentInfo_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;
            Student st = (Student)btn.Tag;
            StudentInfoWindow siw = new StudentInfoWindow(st);
            siw.ShowDialog();
        }


        private void EditStudent_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;
            Student st = (Student)btn.Tag;
            
            AddStudentWindow asw = new AddStudentWindow(st);
            asw.Owner = this;
            var res = asw.ShowDialog();
            if (res == true)
            {
                LinksProvider.Context.SaveChanges();
            }
            else
            {
                var stud = LinksProvider.Context.Entry(st);
                stud.CurrentValues.SetValues(stud.OriginalValues);
                stud.State = EntityState.Unchanged;
            }
            grdStudents.ItemsSource = null;
            grdStudents.ItemsSource = GetStudents(txtFilter.Text);
        }

        private void RemoveStudent_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;
            Student st = (Student)btn.Tag;
            LinksProvider.Context.Students.Remove(st);
            LinksProvider.Context.SaveChanges();
            grdStudents.ItemsSource = GetStudents(txtFilter.Text);
        }


        private void AddTeacher_Click(object sender, RoutedEventArgs e)
        {
            Teacher teacher = new Teacher();

            AddTeacherWindow atw = new AddTeacherWindow(teacher);
            atw.Owner = this;
            var res = atw.ShowDialog();
            if (res == true)
            {
                LinksProvider.Context.Teachers.Add(teacher);
                LinksProvider.Context.SaveChanges();
                grdTeachers.ItemsSource = null;
                grdTeachers.ItemsSource = GetTeachers();
            }

        }

        private void EditTeacher_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;
            Teacher teacher = (Teacher)btn.Tag;

            AddTeacherWindow atw = new AddTeacherWindow(teacher);
            atw.Owner = this;
            var res = atw.ShowDialog();
            if (res == true)
            {
                LinksProvider.Context.SaveChanges();
            }
            else
            {
                var t = LinksProvider.Context.Entry(teacher);
                t.CurrentValues.SetValues(t.OriginalValues);
                t.State = EntityState.Unchanged;
            }

            grdTeachers.ItemsSource = GetTeachers(teacherFilter.Text);
        }

        private void RemoveTeacher_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;
            Teacher teacher = (Teacher)btn.Tag;
            LinksProvider.Context.Teachers.Remove(teacher);
            LinksProvider.Context.SaveChanges();
            grdTeachers.ItemsSource = GetTeachers(teacherFilter.Text);
        }

        private void TeacherInfo_Click(object sender, RoutedEventArgs e)
        {
            Button btn = (Button)e.OriginalSource;
            Teacher t = (Teacher)btn.Tag;
            TeacherInfoWindow tiw = new TeacherInfoWindow(t);
            tiw.ShowDialog();
        }

        private void Filter_Changed(object sender, TextChangedEventArgs e)
        {
            grdStudents.ItemsSource = null;
            grdStudents.ItemsSource = GetStudents(txtFilter.Text);
        }

        private void TeacherFilter_Changed(object sender, TextChangedEventArgs e)
        {
            grdTeachers.ItemsSource = null;
            grdTeachers.ItemsSource = GetTeachers(teacherFilter.Text);
        }

        private void IsArchieved_Click_1(object sender, RoutedEventArgs e)
        {
            grdStudents.ItemsSource = null;
            grdStudents.ItemsSource = GetStudents(IsArchieved.IsChecked.Value);
        }

        private void TabItem_GotFocus_1(object sender, RoutedEventArgs e)
        {

        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            currentDate = currentDate.AddMonths(-1);
            lblMonth.Content = GlobalSettings.Months[currentDate.Month];
            calendar.CurrentDate = currentDate;
            calendar.Schedules = LinksProvider.Context.Shedules.ToList();
        }

        private void Button_Click_2(object sender, RoutedEventArgs e)
        {
            currentDate = currentDate.AddMonths(1);
            lblMonth.Content = GlobalSettings.Months[currentDate.Month];
            calendar.CurrentDate = currentDate;
            calendar.Schedules = LinksProvider.Context.Shedules.ToList();
        }


    }
}
